﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;


namespace GestiuneMasini
{
    public partial class Form1 : Form
    {

        OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\db\Masini.accdb;Persist Security Info=True");
        OleDbDataAdapter oleDbAdapter;
        DataSet ds;
        OleDbCommand command;
           
        public Form1()
        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            showOff();

        }

        public void showOff()
        {
            if (connection.State.ToString().Equals("Closed"))
            {
                connection.Open();
            }

            oleDbAdapter = new OleDbDataAdapter();
            ds = new DataSet();
            try
            {
                oleDbAdapter.SelectCommand = new OleDbCommand("SELECT * FROM tblMasina", connection);
                dataGridView1.DataSource = null;
                oleDbAdapter.Fill(ds);
                oleDbAdapter.Dispose();
                dataGridView1.DataSource = ds.Tables[0];

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            //if(connection.State.Equals("Open"))
            //{
            //    connection.Close();
            //}
            connection.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            showOff();
        }

        public void clearTxt()
        {
            
            txtAsig.Text = "";
            txtITP.Text = "";
            txtKM.Text = "";
            txtModel.Text = "";
            txtNumar.Text = "";
            txtRov.Text = "";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            clearTxt();
            txtNumar.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value);
            txtModel.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[1].Value);
            txtAsig.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[2].Value);
            txtITP.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[3].Value);
            txtRov.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[4].Value);
            txtKM.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[5].Value);

           
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            String strAsig;
            strAsig = txtAsig.Text;
            DateTime dateAsig;
            dateAsig = DateTime.Parse(strAsig);
            String strITP;
            strITP = txtITP.Text;
            DateTime dateITP;
            dateITP = DateTime.Parse(strITP);
            String strRov;
            strRov = txtRov.Text;
            DateTime dateRov;
            dateRov = DateTime.Parse(strRov);

            String strKM;
            strKM = txtKM.Text;
            Int64 inKM;
            inKM = Convert.ToInt64(strKM);

            String strNumar;
            strNumar = txtNumar.Text.ToString();
            String strModel;
            strModel = txtModel.Text.ToString();
         
            oleDbAdapter.SelectCommand = new OleDbCommand("SELECT * FROM tblMasina", connection);
            oleDbAdapter.Fill(ds);
            oleDbAdapter.Dispose();

            command = new OleDbCommand("INSERT INTO tblMasina VALUES('" + strNumar + "','" + strModel + "','" + dateAsig + "','" + dateITP + "', '" + dateRov + "','" + inKM + "');", connection);
            try
            {
                int temp = command.ExecuteNonQuery();
                if (temp > 0)
                {
                    showOff();
                    MessageBox.Show("Adaugat");

                }
                else
                {
                    MessageBox.Show("Nereusit");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Neinregistrat");
            }
            connection.Close();
            
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            connection.Close();
            Application.Exit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            String strAsig;
            strAsig = txtAsig.Text;
            DateTime dateAsig;
            dateAsig = DateTime.Parse(strAsig);
            String strITP;
            strITP = txtITP.Text;
            DateTime dateITP;
            dateITP = DateTime.Parse(strITP);
            String strRov;
            strRov = txtRov.Text;
            DateTime dateRov;
            dateRov = DateTime.Parse(strRov);

            String strKM;
            strKM = txtKM.Text;
            Int64 inKM;
            inKM = Convert.ToInt64(strKM);

            String strNumar;
            strNumar = txtNumar.Text.ToString();
            String strModel;
            strModel = txtModel.Text.ToString();

        

            string req = "UPDATE tblMasina SET Numar = '" + strNumar + "', Model = '" + strModel + "', expAsigurare = '" + dateAsig + "', expITP = '" + dateITP + "', expRovinieta = '" + dateRov + "', KM = '" + inKM + "' WHERE Numar = '" + txtNumar + "';";
            command = new OleDbCommand(req, connection);
            try
            {
                connection.Open();
                int temp = command.ExecuteNonQuery();
                if (temp > 0)
                {
                    clearTxt();
                    showOff();
                    MessageBox.Show("Modificare Completa");
                }
                else
                {
                    MessageBox.Show("Eroare de modificare");
                }

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
            connection.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            String strNumar;
            strNumar = txtNumar.Text.ToString();

            connection.Open();

            string req = "DELETE FROM tblMasina WHERE Numar = '" + strNumar + "';";
            command = new OleDbCommand(req, connection);
            try
            {
                if (MessageBox.Show("Sunteti sigur ca vreti sa stergeti?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    int temp = command.ExecuteNonQuery();

                    if (temp > 0)
                    {
                        showOff();
                    }
                    else
                    {
                        MessageBox.Show("Eroare in stergere!!");
                    }

                }

            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
            connection.Close();


        }




    }
}
